//
/**
 * Filename: c_signal.c
 * Author:   csbonkers
 * Contact:  forever65535@qq.com
 * Date:     2021/6/19
 * Description:
 *    用sigaction 实现signal signal_intr
 */
#include "apue.h"
Sigfunc *c_signal(int signum, Sigfunc *handler) {
  struct sigaction act, oact;
  act.sa_handler = handler;
  sigemptyset(&act.sa_mask);
  act.sa_flags = 0;
  if (signum == SIGALRM) {
#ifdef SA_INTERRUPT
    act.sa_flags |= SA_INTERRUPT;
#endif
  } else {
    act.sa_flags |= SA_RESTART;
  }
  if (sigaction(signum, &act, &oact) < 0) {
    return SIG_ERR;
  }
  return oact.sa_handler;
}

Sigfunc *c_signal_intr(int signum, Sigfunc *handler) {
  struct sigaction act, oact;
  act.sa_flags = 0;
  act.sa_handler = handler;
  sigemptyset(&act.sa_mask);
#ifdef SA_INTERRUPT
  act.flags |= SA_INTERRUPT;
#endif
  if (sigaction(signum, &act, &oact) < 0) {
    return SIG_ERR;
  }
  return oact.sa_handler;
}
